/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.diego.game.sudoku;

/**
 *
 * @author diego
 */
public class Sudoku {

    public final static int NUMBER_NULL = 19;
    public final static int NUMBER_1 = 1;
    public final static int NUMBER_2 = 2;
    public final static int NUMBER_3 = 3;
    public final static int NUMBER_4 = 4;
    public final static int NUMBER_5 = 5;
    public final static int NUMBER_6 = 6;
    public final static int NUMBER_7 = 7;
    public final static int NUMBER_8 = 8;
    public final static int NUMBER_9 = 9;
    private int x = 1;
    private int y = 1;
    private boolean[][] edits = {
        {true, true, true, true, true, true, true, true, true},
        {true, true, true, true, true, true, true, true, true},
        {true, true, true, true, true, true, true, true, true},
        {true, true, true, true, true, true, true, true, true},
        {true, true, true, true, true, true, true, true, true},
        {true, true, true, true, true, true, true, true, true},
        {true, true, true, true, true, true, true, true, true},
        {true, true, true, true, true, true, true, true, true},
        {true, true, true, true, true, true, true, true, true}
    };
    private int[][] tabuleiro = {
        {NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL},
        {NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL},
        {NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL},
        {NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL},
        {NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL},
        {NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL},
        {NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL},
        {NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL},
        {NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL, NUMBER_NULL}
    };

    public boolean[][] getEdits() {
        return edits;
    }

    public void setEdits(boolean[][] edits) {
        this.edits = edits;
    }

    public int[][] getTabuleiro() {
        return tabuleiro;
    }

    public boolean isCellValida(int number) {
        int[] iArrayHor = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int[] iArrayVer = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        //horizonta
        for (int i = 0; i < this.tabuleiro.length; i++) {
            int indexHorizonta = 9;
            int indexVertical = 9;
            int y1 = this.tabuleiro[i][this.y];
            int x1 = this.tabuleiro[this.x][i];
            if ((y1 == this.y) && (x1 == this.x)) {
                y1 = number;
                x1 = number;
            }
            if (!((x1 == 19) || (x1 == 39))) {
                indexHorizonta = (x1 % 19) % 9;
            }
            if (!((y1 == 19) || (y1 == 39))) {
                indexVertical = (y1 % 19) % 9;
            }
            iArrayHor[indexHorizonta]++;
            iArrayVer[indexVertical]++;
        }
        for (int i = 0; i < this.tabuleiro.length; i++) {
            if (iArrayHor[i] > 1) {
                return false;
            }
            if (iArrayVer[i] > 1) {
                return false;
            }
        }
        return true;
    }

    public boolean isCellValida() {
        int[] iArrayHor = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int[] iArrayVer = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        //horizonta
        for (int i = 0; i < this.tabuleiro.length; i++) {
            int indexHorizonta = 9;
            int indexVertical = 9;
            int y1 = this.tabuleiro[i][this.y];
            int x1 = this.tabuleiro[this.x][i];
            if (!((y1 == 19) || (y1 == 39))) {
                indexHorizonta = (y1 % 19) % 9;
            }
            if (!((x1 == 19) || (x1 == 39))) {
                indexVertical = (x1 % 19) % 9;
            }
            iArrayHor[indexHorizonta]++;
            iArrayVer[indexVertical]++;
        }
        for (int i = 0; i < this.tabuleiro.length; i++) {
            if (iArrayHor[i] > 1) {
                return false;
            }
            if (iArrayVer[i] > 1) {
                return false;
            }
        }
        return true;
    }

    public Sudoku(int[][] tabuleiro) {
        for (int i = 0; i < this.tabuleiro.length; i++) {
            int[] is = tabuleiro[i];
            for (int j = 0; j < is.length; j++) {
                this.tabuleiro[i][j] = tabuleiro[i][j];
                this.edits[i][j] = (tabuleiro[i][j] % 19) == 0;
            }

        }
        setXY(4, 4);
    }

    public boolean finish() {
        int[] iArrayHor = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int[] iArrayVer = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        for (int x1 = 0; x1 < this.tabuleiro.length; x1++) {
            int[] is = tabuleiro[x1];
            int indexHorizonta = 9;
            int indexVertical = 9;
            for (int y1 = 0; y1 < is.length; y1++) {
                if (!((y1 == 19) || (y1 == 39))) {
                    indexHorizonta = (y1 % 19) % 9;
                } else {
                    return false;
                }
                if (!((x1 == 19) || (x1 == 39))) {
                    indexVertical = (x1 % 19) % 9;
                } else {
                    return false;
                }
                iArrayHor[indexHorizonta]++;
                iArrayVer[indexVertical]++;
            }
            for (int i = 0; i < this.tabuleiro.length; i++) {
                if (iArrayHor[i] > 1) {
                    return false;
                }
                if (iArrayVer[i] > 1) {
                    return false;
                }
            }

        }
        return true;
    }

    public Sudoku(int[][] tabuleiro, boolean[][] edits) {
        for (int i = 0; i < this.tabuleiro.length; i++) {
            int[] is = tabuleiro[i];
            for (int j = 0; j < is.length; j++) {
                this.tabuleiro[i][j] = tabuleiro[i][j];
                this.edits[i][j] = edits[i][j];
            }

        }
    }

    public void setTabuleiro(int[][] tabuleiro) {
        this.tabuleiro = tabuleiro;
    }

    public void setXY(int x, int y) {
        int celX = x % 9;
        int celY = y % 9;
        if (celY < 0) {
            celY += 9;
        }

        if (celX < 0) {
            celX += 9;
        }

//antes descelecionar o antigo

        int xy = tabuleiro[this.x][this.y];

        if (xy == 38) {
            tabuleiro[this.x][this.y] = 19;
        } else {
            tabuleiro[this.x][this.y] = (xy % 19);
        }
        if (!isCellValida(tabuleiro[this.x][this.y])) {
            tabuleiro[this.x][this.y] = 19;
        }

//depois selecionar o novo
        this.x = celX;
        this.y = celY;
        xy =
                tabuleiro[this.x][this.y];
        tabuleiro[this.x][this.y] = (xy % 19) + 19;
        if (tabuleiro[this.x][this.y] == 19) {
            tabuleiro[this.x][this.y] = 38;
        }

    }

    public Sudoku() {
        setXY(0, 0);
    }

    public boolean setCell(int number) {
        if (!edits[x][y]) {
            return false;
        }
        if (number == 0) {
            tabuleiro[x][y] = 38;
        } else {
            tabuleiro[x][y] = number + 19;
        }

        return edits[x][y];
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

//    public String toString() {
//        String buffer = "";
//
//        int mCol = tabuleiro.length;
//        int mRow = tabuleiro[0].length;
//        for (int row = 0; row < mRow; row++) {
//
//            for (int col = 0; col < mCol; col++) {
//                buffer += "[" + tabuleiro[row][col] +edits[row][col]+ "]";
//            }
//            buffer+='\n';
//        }
//
//        return buffer;
//    }
}
